package com.tebiecloud.act.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.act.client.constants.ResultEnum;
import com.tebiecloud.act.client.model.entity.Act;
import com.tebiecloud.act.client.model.entity.ActGiftPack;
import com.tebiecloud.act.client.model.entity.ActGoodsRef;
import com.tebiecloud.act.client.model.vo.ActInfoVO2;
import com.tebiecloud.act.client.model.vo.ActListVO;
import com.tebiecloud.act.client.service.IActServiceClient;
import com.tebiecloud.act.server.mapper.ActGiftPackMapper;
import com.tebiecloud.act.server.service.ActGiftPackService;
import com.tebiecloud.act.server.service.ActGoodsRefService;
import com.tebiecloud.act.server.service.ActService;
import com.tebiecloud.act.server.service.feign.GoodsServiceClient;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.goods.client.model.entity.GoodsSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/feign/act")
public class ActActController implements IActServiceClient {

    @Autowired
    ActService actService;

    @Autowired
    ActGoodsRefService actGoodsRefService;

    @Autowired
    ActGiftPackService actGiftPackService;

    @Autowired
    GoodsServiceClient goodsServiceClient;

    /**
     * 获取可购买的活动基础信息
     * @param actId
     * @param goodsSpecificationId
     * @return
     */
    @PostMapping("infoActInfoVO2ForBuy")
    @Override
    public ResultBody<ActInfoVO2> getActInfoVO2ForBuy(@RequestParam("actId") long actId,
                                                      @RequestParam("goodsSpecificationId") long goodsSpecificationId) {
        ActInfoVO2 actInfoVO2 = new ActInfoVO2();
        //获取活动信息
        QueryWrapper<Act> actQueryWrapper = new QueryWrapper<>();
        actQueryWrapper.eq("act_id",actId);
        actQueryWrapper.eq("deleted",0);
        Act act = actService.getOne(actQueryWrapper);
        if(act!=null){
            actInfoVO2.setName(act.getName());
            actInfoVO2.setMainPic(act.getMainPic());
        }
        //获取活动规格价格
        QueryWrapper<ActGoodsRef> actGoodsRefQueryWrapper = new QueryWrapper<>();
        actGoodsRefQueryWrapper.eq("act_id",actId);
        actGoodsRefQueryWrapper.eq("spec_id",goodsSpecificationId);
        actGoodsRefQueryWrapper.eq("deleted",0);
        ActGoodsRef actGoodsRef = actGoodsRefService.getOne(actGoodsRefQueryWrapper);
        if(actGoodsRef!=null){
            actInfoVO2.setPrice(actGoodsRef.getPrice());
        }
        ResultBody<List<GoodsSpecification>> goodsSpecificationBody = goodsServiceClient.selectBySpecIds(String.valueOf(goodsSpecificationId));
        if(goodsSpecificationBody !=null && goodsSpecificationBody.getCode() == com.tebiecloud.common.constants.ResultEnum.OK.getCode()){
            List<GoodsSpecification> goodsSpecifications = goodsSpecificationBody.getData();
            if(goodsSpecifications !=null && goodsSpecifications.size()>0){
                actInfoVO2.setSpecName(goodsSpecifications.get(0).getName());
            }
        }
        actInfoVO2.setBenefit(0);
        actInfoVO2.setMaxCoin(0);
        return ResultBodyUtils.ok(actInfoVO2);
    }

    /**
     * 更新库存数量
     * @param actId 活动id
     * @param goodsSpecificationId 爆品活动时，指定规格id
     * @param amount 正数为加库存，负数为减库存
     * @return
     */
    @PostMapping("updateStockQty")
    @Override
    public ResultBody updateStockQty(@RequestParam("actId") long actId,
                                     @RequestParam("goodsSpecificationId") long goodsSpecificationId,
                                     @RequestParam("amount") int amount) {
        Map<String,String> resultMap = actGoodsRefService.updateStockQty(actId,goodsSpecificationId,amount);
        if(resultMap.get("status").equals("error")){
            return ResultBodyUtils.failed(ResultEnum.ACT_STOCKQTY_ERROR.getMessage());
        }
        return ResultBodyUtils.ok();
    }

    @PostMapping("selectActByActIdAndGoodsId")
    @Override
    public ResultBody<ActListVO> selectActByActIdAndGoodsId(@RequestParam("actId") long actId,
                                                            @RequestParam("goodsId") long goodsId) {
        return ResultBodyUtils.ok(actService.selectActByActIdAndGoodsId(actId,goodsId));
    }

    @PostMapping("selectActByActId")
    @Override
    public ResultBody<Act> selectActByActId(@RequestParam("actId") long actId) {
        QueryWrapper<Act> actQueryWrapper = new QueryWrapper<>();
        actQueryWrapper.eq("act_id",actId);
        Act act = actService.getOne(actQueryWrapper);
        if(act == null)return ResultBodyUtils.failed();
        return ResultBodyUtils.ok(act);
    }

    @PostMapping("selectActGift")
    @Override
    public ResultBody<ActGiftPack> selectActGift(long actId) {
        QueryWrapper<ActGiftPack> actGiftPackQueryWrapper = new QueryWrapper<>();
        actGiftPackQueryWrapper.eq("activity_id",actId);
        ActGiftPack actGiftPack = actGiftPackService.getOne(actGiftPackQueryWrapper);
        if(actGiftPack == null)return ResultBodyUtils.failed();
        return ResultBodyUtils.ok(actGiftPack);
    }
}
